package demo.hash;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class RedisDemo {

    public static void test1(Jedis jedis){
        System.out.println("hset 和 hget");
        jedis.flushAll();

        jedis.hset("key", "f1", "11");
        Map<String, String> fields = new HashMap<>();
        fields.put("f2", "22");
        fields.put("f3", "33");
        fields.put("f4", "44");
        jedis.hset("key", fields);

        String result = jedis.hget("key", "f1");
        System.out.println("result = " + result);

        result = jedis.hget("key", "f0");
        System.out.println("result = " + result);
    }


    public static void test2(Jedis jedis){
        System.out.println("hexists");
        jedis.flushAll();

        jedis.hset("key", "f1", "11");
        jedis.hset("key", "f2", "22");
        jedis.hset("key", "f3", "33");
        jedis.hset("key", "f4", "44");

        boolean result = jedis.hexists("key", "f1");
        System.out.println("result = " + result);

        result = jedis.hexists("key", "f0");
        System.out.println("result = " + result);

    }


    public static void test3(Jedis jedis){
        System.out.println("hdel");
        jedis.flushAll();

        jedis.hset("key", "f1", "11");
        jedis.hset("key", "f2", "22");
        jedis.hset("key", "f3", "33");
        jedis.hset("key", "f4", "44");

        long result = jedis.hdel("key", "f1", "f2");
        System.out.println("result = " + result);

        boolean exists = jedis.hexists("key", "f1");
        System.out.println("exists = " + exists);

        exists = jedis.hexists("key", "f2");
        System.out.println("exists = " + exists);
    }


    public static void test4(Jedis jedis){
        System.out.println("hkeys 和 hvals");
        jedis.flushAll();

        jedis.hset("key", "f1", "11");
        jedis.hset("key", "f2", "11");
        jedis.hset("key", "f3", "11");
        jedis.hset("key", "f4", "11");

        Set<String> fields = jedis.hkeys("key");
        List<String> vals = jedis.hvals("key");

        System.out.println("fields = " + fields);
        System.out.println("vals = " + vals);
    }


    public static void test5(Jedis jedis){
        System.out.println("hmget 和 hmset");
        jedis.flushAll();

        Map<String, String> map = new HashMap<>();
        map.put("f1", "11");
        map.put("f2", "22");
        map.put("f3", "33");
        map.put("f4", "44");

        jedis.hmset("key", map);

        List<String> values = jedis.hmget("key", "f1", "f2", "f3", "f4");
        System.out.println("values = " + values);
    }




    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try(Jedis jedis = jedisPool.getResource()){
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
//            test4(jedis);
            test5(jedis);
        }
    }
}
